<?php


namespace app\common\model;

use think\Db;

class VisitRegistration extends Base
{
    // ���õ�ǰģ�Ͷ�Ӧ���������ݱ�����
    protected $table = 'ddt_visitregistration';
    // ����ID
    protected $pk = 'visit_reg_id';

    /**
     * @param array $where  // ��ѯ������
     * @param string $item  // ��ѯ�Ĳ���
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = Db::table($this->table)
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }


    // ��ȡ��ҳ�б�
    function get_list_page($cond,$orderby,$item,$limit)
    {
        $where = null;

        $rs = Db::table($this->table)
            ->field($item)
            ->where($where);
        if(!empty($cond['time'])){
            $array = explode(' - ',$cond['time']);
            $start = strtotime($array[0]." 00:00:00");
            if($array[0] == $array[1]){
                $end = strtotime($array[0]." 23:59:59");
            } else {
                $end = strtotime($array[1]." 00:00:00");
            }
            $rs = $rs->where('add_time','between',[$start,$end]);
        }
        if(!empty($cond['company_name'])){
            $rs = $rs->where('companyname','like','%'.$cond['company_name'].'%');
        }
        $rs = $rs
            ->order($orderby)
            ->paginate($limit)
            ->each(function($items,$key){
                if(!empty($items['industrynumber'])){
                    $hanye_data = [
                        '1' => '酒店餐饮业',
                        '2' => '水产业',
                        '3' => '畜牧业',
                        '4' => '食品批发零售业',
                        '5' => '机器设备',
                        '6' => '冷链运输行业',
                        '7' => '冷冻行业',
                        '8' => '食品生产加工',
                        '9' => '其它',
                    ];
                    $industrynumber = explode(',',$items['industrynumber']);
                    foreach($industrynumber as $val){
                        if($val != 9){
                            $items['industry'][] = $hanye_data[$val];
                        } else {
                            $items['industry'][] = $items['industrytext'];
                        }
                    }
                }
                if(!empty($items['getinformnumber'])){
                    $liaojie_data = [
                        '1' => '抖音',
                        '2' => '亲友介绍',
                        '3' => '微信公众号',
                        '4' => '泰克尼康公司官网',
                        '5' => '其它',
                    ];
                    $getinformnumber = explode(',',$items['getinformnumber']);
                    foreach($getinformnumber as $val){
                        if($val != 5){
                            $items['getinform'][] = $liaojie_data[$val];
                        } else {
                            $items['getinform'][] = $items['getinformtext'];
                        }
                    }
                }
                if(!empty($items['goalnumber'])){
                    $mudi_data = [
                        '1' => '了解冻眠技术、冻眠机器',
                        '2' => '携带食材冻眠试验',
                        '3' => '购买冻眠机器',
                        '4' => '应用冻眠技术',
                        '5' => '其它',
                    ];
                    $goalnumber = explode(',',$items['goalnumber']);
                    foreach($goalnumber as $val){
                        if($val != 5){
                            $items['goal'][] = $mudi_data[$val];
                        } else {
                            $items['goal'][] = $items['goaltext'];
                        }
                    }
                }
                return $items;
            });

        return $rs;
    }

    // ��ȡ�б�
    function get_list($item)
    {
        $rs = Db::table($this->table)
            ->field($item)
            ->select();
        return $rs;
    }

    //�������༭(��̨����)
    function edit($params, $id=0){
        if($id>0){
            // ��������
            $where = ['visit_reg_id' => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // ��������
            $rs = $this->allowField(true)->save($params);
        }
        if ($rs === false) {
            $this->setError(-1, '�ϴ�ʧ��');
            return false;
        }
        return $rs;
    }

}